//用户信息的store

import { defineStore } from "pinia"
import { reactive } from "vue"
import { getAdmin } from "@/api"


export const useAdmin = defineStore("admin", ()=>{
    //定义状态数据(state)
    const admin = reactive({
        id:"",
        username:"",
        password:"",
        salt:"",
        avatar:"",
        permission:"",
        sort:"",
        state:"",
        loginTime:"",
        loginPlace:""
    })

    //修改管理员信息的方法(action)
    const updateAdmin = async ()=>{
        const res = await getAdmin()
        if(res.data.errno == 0){
            admin.id = res.data.data.id
            admin.username = res.data.data.username
            admin.password = res.data.data.password
            admin.salt = res.data.data.salt
            admin.avatar = res.data.data.avatar
            admin.permission = res.data.data.permission
            admin.sort = res.data.data.sort
            admin.state = res.data.data.state
            admin.loginTime = new Date().toLocaleString()
            admin.loginPlace = "北京"
        }
    }

    //移除用户信息的方法(action)
    const removeAdmin = ()=>{
        admin.id = ""
        admin.username = ""
        admin.password = ""
        admin.salt = ""
        admin.avatar = ""
        admin.permission = ""
        admin.sort = ""
        admin.state = ""
    }

    //以对象形式返回
    return { admin, updateAdmin, removeAdmin }
},{
    persist:{
        key:"admin",
        storage:sessionStorage
    }
})
